#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

ll qmi(ll a, ll b)
{
    ll res = 1;
    while(b)
    {
        if(b & 1)res = res * a % mod;
        b >>= 1;
        a = a * a % mod;
    }
    return res;
}
struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		x ^= x << 13;
		x ^= x >> 7;
		x ^= x << 17;
		return x; 
	}
	size_t operator () (uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); // 时间戳
		return splitmix64(x + FIXED_RANDOM);
	}
};

void solve()
{
    int h, n;
    cin >> h >> n;
    ll sum = 0;
    vector<ll> a(n), c(n);
    for(int i = 0; i < n; i++)
    {
        cin >> a[i];
        sum += a[i];
    }
    for(int i = 0; i < n; i++)cin >> c[i];
    if(sum >= h)
    {
        cout << 1 << endl;
    }
    else 
    {
        h -= sum;
        ll l = 1, r = 1e11;
        while(l < r)
        {
            ll mid = (l + r) >> 1;
            auto check = [&](ll x)
            {
                ll res = 0;
                for(int i = 0; i < n; i++)
                {
                    res += ((x / c[i]) * a[i]);
                    // if(res > h) return true;
                }
                return res >= h;
            };
            // cout << l << ' ' << r << ' ' << mid << endl;
            if(check(mid)) r = mid;
            else l = mid + 1;
        }
        cout << l + 1 << endl;
    }

}

int main()
{
    int t;
    cin >> t;
    while(t--) solve();
    return 0;
}